﻿Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports System.Collections
Imports System.Diagnostics
Imports System.DirectoryServices
Imports System.IO
Imports System.Management
Imports System.Security
Imports Microsoft.Web.Administration
Imports Microsoft.Web.Management
Imports System.Data.SqlClient

Public Class Form1
    Delegate Sub SetTextCallback(text As String)

    Public Sub New()


        ' This call is required by the designer.
        InitializeComponent()
        Servers.DataSource = wmi22.Datapod()
        Username.Text = System.Windows.Forms.SystemInformation.UserName.ToString()



    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Server_status_button_Click(sender As Object, e As EventArgs) Handles Server_status_button.Click
        Dim StateS As String = wmi22.CheckPool(Servers.Text)

        If StateS = "OK" Then
            Sev_box.ResetText()
            Sev_box.AppendText(Servers.Text)
            Helath_box.ResetText()
            Helath_box.AppendText(wmi22.CheckPool(Servers.Text))
            Server_State_box.ResetText()
            Server_State_box.AppendText("Online")
            Attention_Checker.ResetText()
            Attention_Checker.AppendText(wmi22.CheckAttenion(Servers.Text))

        ElseIf StateS = "NOT OK" Then
            Sev_box.ResetText()
            Sev_box.AppendText(Servers.Text)
            Helath_box.ResetText()
            Helath_box.AppendText(wmi22.CheckPool(Servers.Text))
            Server_State_box.ResetText()
            Server_State_box.AppendText("Online")
            Attention_Checker.ResetText()
            Attention_Checker.AppendText(wmi22.CheckAttenion(Servers.Text))
        Else

            MessageBox.Show((Servers.Text))
            Sev_box.ResetText()
            Sev_box.AppendText(Servers.Text)
            Helath_box.ResetText()
            Helath_box.AppendText("Not Reachable")
            Server_State_box.ResetText()
            Server_State_box.AppendText("Not Reachable")
            Attention_Checker.ResetText()
            Attention_Checker.AppendText(wmi22.CheckAttenion(Servers.Text))

        End If

    End Sub

    Private Sub Export_regis_button_Click(sender As Object, e As EventArgs) Handles Export_regis_button.Click
        wmi22.ExportReg("HKEY_LOCAL_MACHINE\\Software\\RCI", "C:\\myLocal.reg", Servers.Text)
    End Sub

    Private Sub Server_Online_button_Click(sender As Object, e As EventArgs) Handles Server_Online_button.Click
        wmi22.ServerOnline(Sev_box.Text)
    End Sub

    Private Sub Server_Offlie_button_Click(sender As Object, e As EventArgs) Handles Server_Offlie_button.Click
        wmi22.ServerOffline(Sev_box.Text)
    End Sub

    Private Sub iss_reset_Click(sender As Object, e As EventArgs) Handles iss_reset.Click
        wmi22.IISReset(Sev_box.Text)


    End Sub

    'Added Cross threading to speed up process and added a background worker 
    Private Sub ThreadProcSafe()
        Me.SetText(Servers.Text)
    End Sub

    'Added Cross threading to speed up process and added a background worker
    Private Sub SetText(text As String)
        ' InvokeRequired required compares the thread ID of the 
        ' calling thread to the thread ID of the creating thread. 
        ' If these threads are different, it returns true. 
        If Me.Servers.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetText)
            Me.Invoke(d, New Object() {text})
        Else
            Me.Servers.Text = text
        End If
    End Sub

    'Added Cross threading to speed up process and added a background worker
    Private Sub backgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
        Dim FullComputerName As String = Servers.Text
        Dim Name As String = Username.Text
        Dim PW As String = Passwords.Text


        'Loops through Counters, but it takes for ever to go through each call. 
        For i As Integer = 0 To 0
            ARTEXT.ResetText()
            ARTEXT.AppendText(wmi22.PerformanceMonitor(FullComputerName, Name, PW, "1"))
            ARTEXTSEC.ResetText()
            ARTEXTSEC.AppendText(wmi22.PerformanceMonitor(FullComputerName, Name, PW, "2"))
            REQWT.ResetText()
            REQWT.AppendText((wmi22.PerformanceMonitor(FullComputerName, Name, PW, "3")))
            REQEXC.ResetText()
            REQEXC.AppendText(wmi22.PerformanceMonitor(FullComputerName, Name, PW, "4"))
            SSA.Clear()
            SSA.AppendText(wmi22.PerformanceMonitor(FullComputerName, Name, PW, "5"))
            AVGDL.ResetText()

            AVGDL.AppendText(wmi22.PerformanceMonitor(FullComputerName, Name, PW, "6"))
        Next


    End Sub


End Class
